package com.example.softwareengg;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Screenin extends Activity implements OnClickListener {

	DatagramSocket ds;
	DatagramPacket dp;
	EditText et;
	Button b;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screenin);
		et=(EditText)findViewById(R.id.editText1);
		b=(Button)findViewById(R.id.button1);
		TextView logout = (TextView) findViewById(R.id.logout);
		
		try {
			ds=new DatagramSocket(2700);
			dp=new DatagramPacket(new byte[512],512);
			dp.setPort(1500);
			dp.setAddress(InetAddress.getByName("192.168.37.255"));
			
		} catch (SocketException e2) {
		// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		b.setOnClickListener(this);
		logout.setOnClickListener(this);
	}
	
		
			@Override
			public void onClick(View v) {
				 switch (v.getId()) {
			        case R.id.logout:
			        	 Intent intent1 = new Intent(/*FirstActivity.this*/ v.getContext(), ScreenA.class);
						    startActivity(intent1);
						    	break;
				// TODO Auto-generated method stub
			        case R.id.button1:
				byte [] buf =new byte[20];
				
				buf=et.getText().toString().trim().getBytes();
				
			
				try {
					dp.setData(buf);
					ds.send(dp);
					Toast.makeText(getApplicationContext(), "File Request Sent :) ;)", Toast.LENGTH_LONG).show();
					Thread.sleep(500);
				} catch (SocketException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			break;
					default:
						break;
					
				
				
			}
				 
		}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
